import React, {useEffect, useState} from 'react'
import {connect} from "react-redux";
import Home from '../../views/sandbox/home/Home'
import Nopermission from '../../views/sandbox/nopermission/NoPermission'
import RightList from '../../views/sandbox/right-manage/RightList'
import RoleList from '../../views/sandbox/right-manage/RoleList'
import UserList from '../../views/sandbox/user-manage/UserList'
import { Switch, Route, Redirect } from 'react-router-dom'
import NewsAdd from '../../views/sandbox/news-manage/NewsAdd'
import NewsDraft from '../../views/sandbox/news-manage/NewsDraft'
import NewsCategory from '../../views/sandbox/news-manage/NewsCategory'
import Audit from '../../views/sandbox/audit-manage/Audit'
import AuditList from '../../views/sandbox/audit-manage/AuditList'
import Unpublished from '../../views/sandbox/publish-manage/Unpublished'
import Published from '../../views/sandbox/publish-manage/Published'
import Sunset from '../../views/sandbox/publish-manage/Sunset'
import NewsPreview from '../../views/sandbox/news-manage/NewsPreview'
import axios from "axios";
import NewsUpdate from "../../views/sandbox/news-manage/NewsUpdate";
import {Spin} from "antd";

const LocalRouterMap = {
    "/home":Home,
    "/user-manage/list":UserList,
    "/right-manage/role/list":RoleList,
    "/right-manage/right/list":RightList,
    "/news-manage/add":NewsAdd,
    "/news-manage/draft":NewsDraft,
    "/news-manage/category":NewsCategory,
    "/news-manage/preview/:id":NewsPreview,//新闻预览  :id动态路由
    "/news-manage/update/:id":NewsUpdate,//新闻更新  :id动态路由
    "/audit-manage/audit":Audit,
    "/audit-manage/list":AuditList,
    "/publish-manage/unpublished":Unpublished,
    "/publish-manage/published":Published,
    "/publish-manage/sunset":Sunset
}

function NewsRouter(props) {
    //需要两个平级的路径表，即是 rights表与children表
    /**
     * 处理多个异步处理：比如说一个页面上需要等两个或多个ajax的数据回来以后才正常显示。
     *
     * Promise.all可以将多个Promise实例包装成一个新的Promise实例。
     * 成功的时候返回的是一个结果数组，而失败的时候则返回最先被reject失败状态的值。
     * */
    /*
    *   目的：完成所有路由，且用户无法通过输入url跳转没有权限的路由
    * */
    const[RightList,setRightList]=useState([])
    useEffect(()=>{
        Promise.all([axios.get("/rights"),
            axios.get("/children")
        ]).then(res=>{
            setRightList([...res[0].data,...res[1].data])
        })
    },[])
    const {role:{rights}}=JSON.parse(localStorage.getItem("token"))

    const checkRouter = (item) =>{//如果该路由存在，且权限列表的页面配置项是开启的
        return LocalRouterMap[item]!==null && (item.pagepermisson===1||item.routepermisson===1);
    }
    const checkPermission = (item) => {//当前用户的权限是否包含该路径
        return  rights.includes(item.key)
    }
    return (
        <Spin size="large" spinning={props.isLoadding}>
            <Switch>
                {
                    RightList.map(item=> {
                        if (checkRouter(item) && checkPermission(item))
                            return <Route path={item.key} key={item.key} component={LocalRouterMap[item.key]}exact/>
                    })
                }
                <Redirect from="/" to="/home" exact />
                {
                    RightList.length>0&&<Route path="*" component={Nopermission} />
                    //因为一开始是空数组，所有加个判断防止刚开始闪出Nopermission
                }

            </Switch>
        </Spin>

    )
}
export default connect((state)=>{
    const {loadding:{isLoadding}}=state
    return{
        isLoadding
    }
},)(NewsRouter)